package br.com.news.mail;

import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import br.com.news.rss.RmiFactory;
import br.com.news.utils.database.beans.AnuncioBean;
import br.com.news.utils.service.exception.ServiceException;

public class JobSendAnuncio extends QuartzJobBean {
	@Autowired
    private EmailSenderAnuncio emailSender;

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		
		List<AnuncioBean> anuncios = RmiFactory.getAnuncioService().buscaAnunciosNaoEnviados();
		
		for (AnuncioBean anuncio : anuncios) {
			emailSender.sendEmail("alexandreudicf@gmail.com", "alexandreudicf@gmail.com",anuncio.getNome()+" acaba de enviar proposta de anuncio",anuncio);
			anuncio.setFlagEnvioEmail(true);
			
			try {
				RmiFactory.getAnuncioService().update(anuncio);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
	}

	public void setEmailSender(EmailSenderAnuncio emailSender) {
		this.emailSender = emailSender;
	}
	
	
}
